﻿using System;
using System.IO;
using AppCommon;
using Controller;
using Model;

public partial class ACPanel_NodeMedia_AddEditAlbum : System.Web.UI.Page
{
    public static string _id;
    public static string _idAlbumAnh = "VYCAlbum" + Common.ConvertDateTimetoString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDatatoDDL.BindSubLoaiChuyenMuc(ddlSubloaichuyenmuc);
            BindDatatoDDL.BindSubLoaiTourAll(ddlSubloaitour);
            // BindDatatoDDL.BindDiaDiem(ddlDiadiem);
            var id = Request.QueryString["Id"];
            if (id != null)
            {
                // Thi goi BindData de cap nhat du lieu
                _id = id;
                BindData(_id);
            }
        }
    }

    public void BindData(string id)
    {
        var controller = new VYC21Controller();
        var list = controller.GetById(id);
        if (list.Count > 0)
        {
            tbTen.Text = list[0].Ten;
            ddlSubloaichuyenmuc.SelectedValue = list[0].IdSubLoaiChuyenMuc.ToString();
            ddlSubloaitour.SelectedValue = list[0].IdSubLoaiTour.ToString();
            imgPopup.ImageUrl = Constants.UPLOAD_ALBUMANH + list[0].AnhDaiDien;
            imgPopup.AlternateText = list[0].AnhDaiDien;
            tbTomtat.Text = list[0].TomTat;
            
            cbTrangThai.Checked = Convert.ToBoolean(list[0].TrangThai);
        }
        else
        {
            // Du lieu khong ton tai
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Dữ liệu không tồn tại..</p>";
        }
    }

    protected void btnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var controller = new VYC21Controller();
            var model = new ModelVYC21();

            model.Ten = tbTen.Text;
            model.TomTat = tbTomtat.Text;            
            model.TrangThai = Convert.ToInt32(cbTrangThai.Checked);
            if (imgUploadAnhdaidien.HasFile)
            {
                var pathAnhbia = Server.MapPath(Constants.UPLOAD_ALBUMANH);
                if (!Directory.Exists(pathAnhbia))
                {
                    Directory.CreateDirectory(pathAnhbia);
                }
                // validate file image
                string[] validFileTypes = { "gif", "png", "jpg", "jpeg" };
                var extFile = Path.GetExtension(imgUploadAnhdaidien.PostedFile.FileName);
                var isValidate = false;

                if (extFile != null)
                {
                    foreach (var type in validFileTypes)
                    {
                        if (extFile.Equals("." + type))
                        {
                            isValidate = true;
                            break;
                        }
                    }
                }

                if (!isValidate)
                {
                    litResult.Text = "Invalid file with extension!";
                    model.AnhDaiDien = Constants.NO_IMAGE;
                }
                else
                {

                    litResult.Text = "Ok!";
                    // Luu file 
                    imgUploadAnhdaidien.SaveAs(pathAnhbia + "VYCAlbumAnh-" + DateTime.Now.ToString("ddMMyyhhmmss") +
                                    extFile);
                    model.AnhDaiDien = "VYCAlbumAnh-" + DateTime.Now.ToString("ddMMyyhhmmss") + extFile;
                }
            }
            else
            {
                model.AnhDaiDien = imgPopup.AlternateText;
                // model.AnhDaiDien = Constants.NO_IMAGE;
            }

            model.IdSubLoaiChuyenMuc = Convert.ToInt32(ddlSubloaichuyenmuc.SelectedValue);
            model.IdSubLoaiTour = Convert.ToInt32(ddlSubloaitour.SelectedValue);            


            var isSuccess = 0;
            if (!string.IsNullOrEmpty(_id))
            {
                model.Id = _id;
                isSuccess = controller.EditData(model);
            }
            else
            {
                model.Id = _idAlbumAnh;
                isSuccess = controller.AddData(model);
            }
            if (isSuccess == 0)
            {
                // Chua thanh cong
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-success\">Thêm dữ liệu thành công.</p>";
                if (!string.IsNullOrEmpty(_id))
                {
                    Response.Redirect("~/ACPanel/NodeMedia/AddEditPhotos.aspx?Id=" + _id);
                }
                else
                {
                    Response.Redirect("~/ACPanel/NodeMedia/AddEditPhotos.aspx?Id=" + _idAlbumAnh);
                }
                
                // ResetInput();
            }
        }
        catch (Exception)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            throw;
        }
    }

    public void ResetInput()
    {
        tbTen.Text = "";
        tbTomtat.Text = "";
        cbTrangThai.Checked = false;
        _id = String.Empty;
    }
}